const cookie = require('./cookie');

const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}
const getUserInfo = () => {
  return new Promise(resolve => {
    cookie.getCookie('userInfo', function (res) {
      if (res) {
        var etime = Date.parse(new Date()) / 1000;
        if (etime < res.expiretime) {
          resolve(res)
          // that.getUserInfo();
        }
      }
    })
  })
}
const getToken = () => {
  return new Promise((resolve, reject) => {
    cookie.getCookie('userInfo', function (res) {
      console.log('getToken', res)
      if (!res) return resolve()
      var etime = Date.parse(new Date()) / 1000;
      if (etime < res.expiretime) {
        resolve(res.token)
      }
    })
  })
}
const timeDiff = orderTimeStr=> {
  const orderTime = new Date(orderTimeStr.replace(/-/g, '/'));
  const now = new Date();
  const timeDiff = now - orderTime; // 时间差，单位为毫秒
  const timeDiffInMinutes = timeDiff / (1000 * 60); // 将时间差转换为分钟
  console.log('timeDiffInMinutes:'+timeDiffInMinutes);
  return timeDiffInMinutes > 30;
}
module.exports = {
  formatTime,
  getUserInfo,
  getToken,
  timeDiff
}